package com.zxl.yjb.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;


import com.zxl.yjb.R;

import java.util.List;



/**
 * Created by Administrator on 2017/6/12.
 */

public class ShowImageAdapter extends BaseAdapter {
    protected List<Bitmap> data;
    protected LayoutInflater inflater;
    protected Context context;

    public ShowImageAdapter(List<Bitmap> data, Context context) {
        this.data = data;
        inflater = LayoutInflater.from(context);
        this.context=context;
    }

    private OnDeleteImgListener deleteImg;

    public void setDeleteImg(OnDeleteImgListener deleteImg) {
        this.deleteImg = deleteImg;
    }

    public interface OnDeleteImgListener{
        void delete(int position);
    }
    @Override
    public int getCount() {
        if(data.size()==6){
            return 6;
        }
        return (data.size()+1);
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder=null;
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView=inflater.inflate(R.layout.show_publish_img_item, null);
            viewHolder.iv_delete= (ImageView) convertView.findViewById(R.id.iv_delete);
            viewHolder.iv_add= (ImageView) convertView.findViewById(R.id.iv_content);
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }
        if(position==data.size()){

            viewHolder.iv_add.setImageBitmap(BitmapFactory.decodeResource(context.getResources(),R.mipmap.add_img));

            viewHolder.iv_delete.setVisibility(View.GONE);
            if (position>=6){
                viewHolder.iv_add.setVisibility(View.GONE);
            }
        }else{
//            viewHolder.iv_delete.setVisibility(View.VISIBLE);

            viewHolder.iv_add.setImageBitmap(data.get(position));
        }
        /*viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteImg.delete(position);
            }
        });*/
        return convertView;
    }


    public void setGridViewData(List<Bitmap> bitmapList) {
        this.data = bitmapList;
        this.notifyDataSetChanged();
    }


    public static class ViewHolder{
        ImageView iv_delete,iv_add;
    }
}
